﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Core;
using Portal.Exam.Model;

namespace Portal.Exam.Render
{
    public class ExamResult
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();


            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string SelectExamType =
                      "<select id=drpSelectExamType onchange=\"javascript:DetectExamSegment();\">\r\n" +
                      "   <option value=\"exam\">Thi thật</option>\r\n" +
                      "   <option value=\"test\">Thi thử</option>\r\n" +
                      "</select>\r\n";

                string RetHtml =
                    " <script language=javascript> \r\n" +
                    "   function ClientReadingExamListStudent()\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       if(document.getElementById(\"drpSelectExamSegment\")==null)\r\n" +
                    "       {\r\n" +
                    "           alert('Không có kỳ thi. Xin vui lòng chọn kỳ thi!');\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       ExamSegmentId = document.getElementById(\"drpSelectExamSegment\").value; \r\n" +
                    "       ExamRoomNo = document.getElementById(\"txtExamRoomNo\").value; \r\n" +
                    "       AjaxOut = Portal.Exam.Render.ExamResult.ServerSideReadingExamRoomStudent(SiteId, ExamSegmentId, ExamRoomNo).value; \r\n" +
                    "       document.getElementById('divListExamRoomStudent').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "   }\r\n" +

                    "   function PrintExamResultList()\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       if(document.getElementById(\"drpSelectExamSegment\")==null)\r\n" +
                    "       {\r\n" +
                    "           alert('Không có kỳ thi. Xin vui lòng chọn kỳ thi!');\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       ExamSegmentId = document.getElementById(\"drpSelectExamSegment\").value; \r\n" +
                    "       ExamRoomNo = document.getElementById(\"txtExamRoomNo\").value; \r\n" +
                    "       AjaxOut = Portal.Exam.Render.ExamResult.ServerSidePrint(SiteId, ExamSegmentId, ExamRoomNo).value; \r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_blank');\r\n" +
                    "   }\r\n" +



                    "   function DetectExamSegment()\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       ExamType = document.getElementById(\"drpSelectExamType\").value; \r\n" +
                    "       AjaxOut = Portal.Exam.Render.ExamResult.ServerSideReadingExamSegment(SiteId,ExamType).value; \r\n" +
                    "       document.getElementById('divSegment').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "       JumPageExamRoom(0);\r\n" +
                    "   }\r\n" +
                    " </script>\r\n" +
                    " <input id=txtPageIndex type=hidden value=\"0\">\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:PrintExamResultList();\"><img class=image src=\"" + IconRoot + "/Print.png\"><div>In danh sách</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=exam','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "  <div class=\"tabFunction\">Hình thức: " + SelectExamType + " / Kỳ thi: <span id=divSegment>" + ServerSideReadingExamSegment(SiteId, "exam").HtmlContent + "</span>Phòng thi: <input id=txtExamRoomNo value=\"\"><input type=button class=button value=\"Lọc\" onclick=\"javascript:ClientReadingExamListStudent();\";></div>\r\n" +
                    "  <div id=divProcessing></div>" +
                    "  <div id=\"divListExamRoomStudent\" class=tabcontent><div class=CssNoData>Nhập các tiêu chí để lọc danh sách thí sinh trong phòng thi</div></div>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingExamRoomStudent(
            string SiteId,
            string ExamSegmentId,
            string ExamRoomNo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (string.IsNullOrEmpty(ExamRoomNo)) throw new Exception("Chưa nhập phòng thi để lọc!");
                ExamRoomCls OExamRoom = new ExamRoomBll().CreateModel(SiteId, ExamSegmentId, ExamRoomNo);
                if (OExamRoom == null) throw new Exception("Không tìm thấy phòng thi thuộc đợt thi này!");
                DataTable dtExamRoomStudent = new ExamRoomStudentBll().ReadingExamRoomStudentByExamRoomNo(SiteId, ExamSegmentId, ExamRoomNo);
                if (dtExamRoomStudent.Rows.Count == 0)
                {
                    OAjaxOut.HtmlContent =
                        "  <div class=CssNoData>Không có phòng thi nào thỏa mãn điều kiện lọc</div>";
                    return OAjaxOut;
                }

                string ExamInfo = OExamRoom.RoomNo + " / " + OExamRoom.ExamSubject + " / " + OExamRoom.ExamSegmentName;
                string RetHtml =
                    "   <div class=PageTitle>Số sinh viên trong phòng thi " + ExamInfo + " / tổng số có (" + dtExamRoomStudent.Rows.Count.ToString("#,##0") + ") thí sinh</div>" +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <th class=thIndex>#</th>" +
                    "           <th>Mã SV</th>" +
                    "           <th>Tên sinh viên</th>" +
                    "           <th>Giới tính</th>" +
                    "           <th>Ngày sinh</th>" +
                    "           <th>Khóa</th>" +
                    "           <th>Lớp</th>" +
                    "           <th>Ngành</th>" +
                    "           <th>Đề</th>" +
                    "           <th>Trạng thái</th>" +
                    "           <th>Số điểm</th>" +
                    "       </tr>";

                for (int iIndex = 0; iIndex < dtExamRoomStudent.Rows.Count; iIndex++)
                {
                    string ExamRoomStudentId = (string)dtExamRoomStudent.Rows[iIndex]["ExamRoomStudentId"];
                    string StudentCode = (string)dtExamRoomStudent.Rows[iIndex]["StudentCode"];
                    string StudentName = (string)dtExamRoomStudent.Rows[iIndex]["StudentName"];
                    int Sex = (int)dtExamRoomStudent.Rows[iIndex]["Sex"];
                    int FinishExam = (int)dtExamRoomStudent.Rows[iIndex]["FinishExam"];
                    int HasGenQuestion = (int)dtExamRoomStudent.Rows[iIndex]["HasGenQuestion"];
                    DateTime StartExam = new System.DateTime();
                    if (dtExamRoomStudent.Rows[iIndex]["StartExam"] != DBNull.Value)
                    {
                        StartExam = (DateTime)dtExamRoomStudent.Rows[iIndex]["StartExam"];
                    }
                    string Status = "Chưa thi";
                    if (StartExam != new System.DateTime())
                    {
                        if (FinishExam == 0)
                        {
                            Status = "Đang thi";
                        }
                        else
                        {
                            Status = "Thi xong";
                        }
                    }

                    string sDob = "";
                    try
                    {
                        DateTime Dob = (DateTime)dtExamRoomStudent.Rows[iIndex]["Dob"];
                        sDob = Dob.ToString("dd/MM/yyyy");
                    }
                    catch { }
                    string Course = (string)dtExamRoomStudent.Rows[iIndex]["Course"];
                    string TrainingLevel = (string)dtExamRoomStudent.Rows[iIndex]["TrainingLevel"];
                    string Class = (string)dtExamRoomStudent.Rows[iIndex]["Class"];
                    int DonePoint = 0;
                    if (dtExamRoomStudent.Rows[iIndex]["DonePoint"] != DBNull.Value)
                    {
                        DonePoint = (int)dtExamRoomStudent.Rows[iIndex]["DonePoint"];
                    }
                    int TotalPoint = 0;
                    if (dtExamRoomStudent.Rows[iIndex]["TotalPoint"] != DBNull.Value)
                    {
                        TotalPoint = (int)dtExamRoomStudent.Rows[iIndex]["TotalPoint"];
                    }
                    string DonePointText = DonePoint.ToString() + "/" + TotalPoint.ToString();
                    //int FinishExam = (int)dtExamRoomStudent.Rows[iIndex]["FinishExam"];
                    if (FinishExam == 0)
                    {
                        DonePointText = "chưa có";
                    }
                    else
                    {
                        string Url = "ViewResultExamStudent.aspx?ExamRoomStudentId=" + ExamRoomStudentId;
                        DonePointText = "<a href=\"" + Url + "\">" + DonePointText + "</a>\r\n";
                    }
                    RetHtml +=
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td class=tdIndex>" + (iIndex + 1) + "</td>" +
                    "           <td class=tdCode>" + StudentCode + "</td>" +
                    "           <td>" + StudentName + "</td>" +
                    "           <td>" + (Sex == 1 ? "Nam" : "Nữ") + "</td>" +
                    "           <td class=tdDate>" + sDob + "</td>" +
                    "           <td>" + Course + "</td>" +
                    "           <td>" + Class + "</td>" +
                    "           <td>" + TrainingLevel + "</td>" +
                    "           <td>" + (HasGenQuestion == 1 ? "Đã tạo" : "Chưa tạo") + "</td>" +
                    "           <td>" + Status + "</td>" +
                    "           <td>" + DonePointText + "</td>" +

                    //"           <td class=tdButton><input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdateExamRoomStudent('" + ExamRoomStudentId + "','" + ExamRoomStudentId + "');\"></td>" +
                    //"           <td class=tdButton><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDeleteExamRoomStudent('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>" +
                    //"           <td class=tdButton><input type=button class=button value=\"Session\" onclick=\"javascript:ClientSetExamRoomStudentSession('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>" +
                    "       </tr>" +
                    "       <th>" +
                    "       <td>" +
                    "       Ghi chú" +
                    "       </td>" +
                    "       <td colspan=9>" +
                    "       <input type=\"textbox\" id=\"txt_note" + iIndex + "\" class=\"note\"/>" +
                    "       </td>" +
                    "       </th>";
                }
                RetHtml +=
                    "   </table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = RetHtml;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingExamSegment(string SiteId, string ExamType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dt = new ExamSegmentBll().ReadingExamSegment(SiteId, ExamType, 1, "");
                if (dt.Rows.Count == 0)
                {
                    OAjaxOut.HtmlContent = "<span class=CssNoExamSegment style=\"margin-right:5px;color:maroon;font-weight:bold;\">(không có kỳ thi)</span>";
                    return OAjaxOut;
                }
                string SelectExamSegment =
                    "<select id=drpSelectExamSegment>\r\n";
                for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
                {
                    string ExamSegmentId = (string)dt.Rows[iIndex]["ExamSegmentId"];
                    string ExamSegmentName = (string)dt.Rows[iIndex]["ExamSegmentName"];
                    SelectExamSegment += "    <option value=\"" + ExamSegmentId + "\">" + ExamSegmentName + "</option>\r\n";
                }
                SelectExamSegment += "</select>\r\n";
                OAjaxOut.HtmlContent = SelectExamSegment;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSidePrint(
            string SiteId,
            string ExamSegmentId,
            string ExamRoomNo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (string.IsNullOrEmpty(ExamRoomNo)) throw new Exception("Chưa nhập phòng thi để lọc!");
                ExamRoomCls OExamRoom = new ExamRoomBll().CreateModel(SiteId, ExamSegmentId, ExamRoomNo);
                if (OExamRoom == null) throw new Exception("Không tìm thấy phòng thi thuộc đợt thi này!");
                DataTable dtExamRoomStudent = new ExamRoomStudentBll().ReadingExamRoomStudentByExamRoomNo(SiteId, ExamSegmentId, ExamRoomNo);
                if (dtExamRoomStudent.Rows.Count == 0)
                {
                    OAjaxOut.HtmlContent =
                        "  <div class=CssNoData>Không có phòng thi nào thỏa mãn điều kiện lọc</div>";
                    return OAjaxOut;
                }
                OAjaxOut.RetUrl = "PrintExamResultList.aspx?ExamRoomId=" + OExamRoom.ExamRoomId;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
